package com.atguigu.gmall.list.controller;


import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.service.SearchService;
import com.atguigu.gmall.model.list.Goods;
import com.atguigu.gmall.model.list.SearchParam;
import com.atguigu.gmall.model.list.SearchResponseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.redis.connection.ReactiveSubscription;
import org.springframework.web.bind.annotation.*;

/**
 * 搜索管理
 * 添加
 * 修改
 * 删除
 * 查询
 */
@Api(tags = "搜索管理")
@RestController
@RequestMapping("/api/list")
public class ListApiController {

    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Autowired
    private SearchService searchService;

    //ES  mapping映射
    @ApiOperation("初始化//ES  mapping映射")
    @GetMapping("/inner/createIndex")
    public Result createIndex() {
        //骨架映射
        //1:创建索引库    相当于 Mysql中创建数据库
        elasticsearchRestTemplate.createIndex(Goods.class);
        //2:映射索引库   相当于 Mysql中创建表
        elasticsearchRestTemplate.putMapping(Goods.class);
        return Result.ok();
    }

    //后台管理  上架 --》 更新商品上架状态以外 还有什么事情吗？ 将商品的信息保存ES中 否则用户是搜索不到
    @ApiOperation("上架商品数据到索引库")
    @GetMapping("/inner/upperGoods/{skuId}")
    public Result upperGoods(
            @PathVariable(name = "skuId") Long skuId
    ) {
        searchService.upperGoods(skuId);
        return Result.ok();
    }

    //下架商品
    @ApiOperation("下架商品、删除索引库的数据")
    @GetMapping("/inner/lowerGoods/{skuId}")
    public Result lowerGoods(
            @PathVariable(name = "skuId") Long skuId
    ) {
        searchService.lowerGoods(skuId);
        return Result.ok();
    }

    //增加商品的热度
    @ApiOperation("//增加商品的热度")
    @GetMapping("/inner/hosScore/{skuId}")
    public Result hosScore(@PathVariable(name = "skuId") Long skuId) {
        searchService.hosScore(skuId);

        return Result.ok();
    }

    @ApiOperation("根据入参对象搜索索引库并返回结果")
    @PostMapping("/search")
    public Result search(@RequestBody SearchParam searchParam) throws Exception{
        //执行搜索
        SearchResponseVo vo = searchService.search(searchParam);
        return Result.ok(vo);
    }

}
